package com.tangr.normaladapter.abslistview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.tangr.normaladapter.ViewHolder;

import java.util.List;

/**
 * Created by tangr on 2016/6/7.
 */
public abstract class MultiItemAdapter<T> extends NormalAdapter<T>{
    protected MultiItemTypeSupport<T> mMultiItemTypeSupport;
    public MultiItemAdapter(Context context, List<T> list, MultiItemTypeSupport<T> multiItemTypeSupport) {
        super(context, list, -1);
        mMultiItemTypeSupport = multiItemTypeSupport;
        if (mMultiItemTypeSupport == null)
            throw new IllegalArgumentException("the mMultiItemTypeSupport can not be null.");
    }

    @Override
    public int getItemViewType(int position) {
        if (mMultiItemTypeSupport != null)
            return mMultiItemTypeSupport.getItemViewType(position,
                    mList.get(position));
        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        if (mMultiItemTypeSupport != null)
            return mMultiItemTypeSupport.getViewTypeCount();
        return super.getViewTypeCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mMultiItemTypeSupport == null)
            return super.getView(position, convertView, parent);
        int layoutid = mMultiItemTypeSupport.getLayoutId(getItemViewType(position));
        ViewHolder vh = ViewHolder.get(mContext,convertView,parent,layoutid);
        setView(vh,mList.get(position));
        return vh.getConvertView();
    }
}
